package com.cdream.basic.redis.config;

import com.cdream.basic.redis.config.model.RedisTestModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author cdream
 * @date 7/7/21
 */
@RestController
@RequestMapping("/redis")
public class RedisExampleController {
    @Autowired
    private HttpServletRequest httpServletRequest;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    /**
     * 储存数据到session
     * @return
     */
    @RequestMapping("/save_to_session")
    public Object saveToSession(){
        this.httpServletRequest.getSession().setAttribute("isLogin",true);
        return "success";
    }
    /**
     * 储存数据到session
     */
    @RequestMapping("/get_session")
    public Object getSession(){
        return this.httpServletRequest.getSession().getAttribute("isLogin");
    }

    @RequestMapping("/save_by_redis_template")
    public void saveByRedisTemplate(){
        RedisTestModel redisTestModel = new RedisTestModel();
        redisTestModel.setName("zs");
        redisTestModel.setAge(12);
        redisTestModel.setDate(new Date());
        redisTemplate.opsForValue().set("test1",redisTestModel);
    }
    @RequestMapping("/get_by_redis_template")
    public Object getByRedisTemplate(){
        RedisTestModel r = (RedisTestModel) redisTemplate.opsForValue().get("test1");
        return r;
    }



}
